<html>
<textarea id="raw" rows="70" cols="80">
</textarea>
<textarea id="result" rows="70" cols="80">
At Jerry.
</textarea>
<button onclick="myFunction()">Click me</button>
<script>

function getNodeTobeReplaced(current, parent, newPicUrl ){
	return {
		"current": current,
		"parent": parent,
		"newPicUrl": newPicUrl
	};
}

function getNewPicture(index, picRepository){
	var pictureIndex = index + 1;
	var searchKey = "![clipboard" + pictureIndex + "](";
	var searchKeyLength = searchKey.length;
	var startIndex = picRepository.indexOf(searchKey);
	console.assert( startIndex > -1);
	var currentImage = picRepository.substring(startIndex);
	var endIndex = currentImage.indexOf(")");
	console.assert( endIndex > -1);
	return newPic = currentImage.substring(0, endIndex+1);
}
function myFunction(){
	var source = document.getElementById("raw");
// Jerry 2017-11-05 9:25PM - the input for source.value	is html source code tag like <p>dssaf<p><ac:image>...
	var html = source.value;
	var div = document.createElement('div');
	div.innerHTML = html;
	var elements = div.childNodes;
	var r1 = div.getElementsByTagName("ac:image");
	var imageToRemove = [];
	var picRepository = elements[elements.length - 1].data;
	for( var i = 0; i < r1.length; i++){
		var record = getNodeTobeReplaced(r1[i], r1[i].parentNode,
			getNewPicture(i, picRepository));
		imageToRemove.push(record);
	}
	for( var i = 0; i < imageToRemove.length;i++){
		// Jerry 2017-05-29 14:50PM does not work, a line break is needed here
		var newNode = document.createElement("div");
		newNode.innerHTML = "\n\n" + imageToRemove[i].newPicUrl;
		imageToRemove[i].parent.replaceChild(newNode, imageToRemove[i].current);
	}
	div.removeChild(elements[elements.length - 1]);
	var target = document.getElementById("result");
	target.value = div.innerHTML;
	copyToClipboard(target);
}

function copyToClipboard(node) {
  node.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    alert('Copying text command was ' + msg);
  } catch (err) {
    alert('Oops, unable to copy: ' + msg);
  }
}


</script>
</html>